package com.baosight.sczx.zp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zp.domain.ZPUser;

/**
 * 用户管理
 * <p>Title: ServiceZP09.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-23 上午06:05:58  郭淑芳  Create
 *</pre>
 */
public class ServiceZP09 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP09</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:05:48 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		EiInfo outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		if(inInfo.getString("isUpate").equals("是"))
		{
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("orgCode", inInfo.getString("orgCode"));
			_argMap.put("dept", inInfo.getString("orgName"));
			dao.update("ZPUser.modifyDept", _argMap);//更新user表部门名称
			dao.update("ZPTes01.updateForDepat", _argMap);//更新tes01部门名称
		}
		outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 用户 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP09</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:05:07 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "orgCode", inInfo.getString("orgCode"), true);//添加查询条件 主表ID
		EiInfo outInfo = super.query(inInfo, "ZPUser.query", new ZPUser());
		return outInfo;
	}
	
	
	
	/**
	 * 用户 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP09</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:04:41 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验工号是否重复
		SecurityBridge sbridge = SecurityBridgeFactory.getBridge();
		Map<String, String> params = new HashMap<String, String>();
		params.put("name", inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "name"));
		boolean validateValue = validatePerson(inInfo, "ZPUser.validate", params);
		
		if(validateValue) {
			return inInfo;
		}
		else {			
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "codedPassword", inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "name"));//工号
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "createDate", StaticTools.getDate());	//创建日期
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "pwdExpireDate", "2100-01-01 00:00:00");	//密码过期日期
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "expireDate", "2100-01-01 00:00:00");	//用户过期日期
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "valid", "1");	//是否是用户
			EiInfo outInfo = super.insert(inInfo, "ZPUser.insert");
			String name = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "name");
			sbridge.setUserPassword(name, name);
			boolean insertValue = InsertToTes01(inInfo);//新增人员表记录
			if(!insertValue) {
				outInfo.setStatus(-1);
				outInfo.setMsg("插入TES01操作记录失败！");
				return outInfo;
			}
			return outInfo;
		}
	}
	
	/**
	 * 给tes01表插入记录
	 * <p>Function: InsertToTes01</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 * </p>
	 *
	 */
	public  boolean InsertToTes01(EiInfo inInfo) {
		try {
			String name= inInfo.getCellStr("result", 0, "name");				//工号
			String displayName= inInfo.getCellStr("result", 0, "displayName");	//姓名
			String orgCode= inInfo.getCellStr("result", 0, "orgCode");			//部门ID
			String orgName= inInfo.getCellStr("result", 0, "dept");				//名称
			String fdHandlePeople = UserSession.getInSessionProperty("UserCname").toString();	//操作人Name
			
			if(null == UserSession.getUserId() || null == UserSession.getInSessionProperty("UserCname")) {
				return false;
			}
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("userId", name);
			_argMap.put("userCname", displayName);
			_argMap.put("orgCode", orgCode);
			_argMap.put("orgName", orgName);
			_argMap.put("userClass", FrameworkInfo.getProjectPrefix());
			_argMap.put("recCreator", fdHandlePeople);
			_argMap.put("recCreateTime", StaticTools.getDateDefault());//创建时间
			_argMap.put("recRevisor", " ");
			_argMap.put("recReviseTime", " ");
			_argMap.put("archiveFlag", "1");
			_argMap.put("idcardNo", " ");
			dao.insert("ZPTes01.insert", _argMap);
		} catch (Exception e) {
			System.out.println("在操作记录 新增TES01记录异常：" + e.getMessage());
			return false;
		} 
		return true;
	}

	/**
	 * 用户 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP09</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:04:30 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		
		EiInfo outInfo = super.update(inInfo, "ZPUser.update");
		boolean updateValue = updateToTes01(inInfo);//新增隐患处理记录
		if(!updateValue) {
			outInfo.setStatus(-1);
			outInfo.setMsg("修改TES01操作记录失败！");
			return outInfo;
		}
		return outInfo;
		
	}
	
	/**
	 * 修改tes01表记录
	 * <p>Function: updateToTes01</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 * </p>
	 *
	 */
	public  boolean updateToTes01(EiInfo inInfo) {
		try {
			for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
			{
			    Map<String, String> _argMap = new HashMap<String, String>();
			    _argMap.put("userCname", inInfo.getBlock("result").getCellStr(i, "displayName"));
				_argMap.put("userId", inInfo.getBlock("result").getCellStr(i, "name"));
				_argMap.put("orgCode", inInfo.getBlock("result").getCellStr(i, "orgCode"));
				_argMap.put("orgName", inInfo.getBlock("result").getCellStr(i, "dept"));
				_argMap.put("recRevisor", UserSession.getInSessionProperty("UserCname").toString());
				_argMap.put("recReviseTime", StaticTools.getDateDefault());//创建时间
				dao.update("ZPTes01.updateForPerson", _argMap);
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 用户 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP09</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:03:39 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo _temp = super.delete(inInfo, "ZPUser.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        boolean deleteValue = deleteToTes01(inInfo);//删除Tes01记录
        boolean deleteResValue = deleteToResourcesetMember(inInfo);//删除T_ES_RESOURCESET_MEMBER记录

		if(!deleteValue ) {
			outInfo.setStatus(-1);
			
			outInfo.setMsg("删除TES01操作记录失败！");
			return outInfo;
		} else if( !deleteResValue ) {
			outInfo.setStatus(-1);
			
			outInfo.setMsg("删除T_ES_RESOURCESET_MEMBER操作记录失败！");
			return outInfo;
		}
		return outInfo;
	}
	/**
	 * 删除T_ES_RESOURCESET_MEMBER表记录
	 * <p>Function: InsertToTes01</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 * </p>
	 *
	 */
	public  boolean deleteToResourcesetMember(EiInfo inInfo) {
		try {
			for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
			{
				String name= inInfo.getBlock("result").getCellStr(i, "name");
			    Map<String, String> _argMap = new HashMap<String, String>();
			    _argMap.put("memberName", name);
			    dao.delete("ZPResourcesetMember.delete", _argMap);
			}
		} catch (Exception e) {
			System.out.println("在操作记录 删除T_ES_RESOURCESET_MEMBER记录异常：" + e.getMessage());
			return false;
		} 
		return true;
	}
	/**
	 * 删除tes01表记录
	 * <p>Function: InsertToTes01</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 * </p>
	 *
	 */
	public  boolean deleteToTes01(EiInfo inInfo) {
		try {
			for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
			{
				String name= inInfo.getBlock("result").getCellStr(i, "name");
			    Map<String, String> _argMap = new HashMap<String, String>();
			    _argMap.put("userId", name);
			    dao.delete("ZPTes01.delete", _argMap);
			}
		} catch (Exception e) {
			System.out.println("在操作记录 删除TES01记录异常：" + e.getMessage());
			return false;
		} 
		return true;
	}

	
	/**
	 * 用户 停用
	 * <p>Function: stop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:03:57 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo stop(EiInfo inInfo) 
	{
		EiInfo _temp = super.update(inInfo, "ZPUser.stop");
		 EiInfo outInfo = this.query(_temp);
		return outInfo;
	}
	
	/**
	 * 用户 启用
	 * <p>Function: start</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:04:13 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo start(EiInfo inInfo) 
	{
		EiInfo _temp = super.update(inInfo, "ZPUser.start");
		 EiInfo outInfo = this.query(_temp);
		return outInfo;
	}
	
	/**
	 * 密码重置
	 * <p>Function: reset</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:06:13 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo reset(EiInfo inInfo) 
	{
		// 密码重置
		String strTemp = null;
		SecurityBridge sbridge = SecurityBridgeFactory.getBridge();
		for (int i = 0; i < inInfo.getBlock("result").getRowCount(); i++) {
			strTemp = inInfo.getBlock("result").getCellStr(i, "name");
			sbridge.setUserPassword(strTemp, strTemp);
		}
		inInfo.set("formStatus", 1);
		return inInfo;
	}
	
	/**
	 * 用户 迁移
	 * <p>Function: move</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:06:28 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo move(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo,"ZPUser.move");
		boolean updateValue = updateToTes01(inInfo);//更新TES01
		if(!updateValue) {
			outInfo.setStatus(-1);
			outInfo.setMsg("修改TES01操作记录失败！");
			return outInfo;
		}
		return outInfo;
	}
	
	/**
	 * 校验工号是否重复
	 * <p>Function: validatePerson</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午06:07:24 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validatePerson(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("工号重复！");
			return true;
		}
		return false;
	}
}
